package com.wkbb.sports.controller;

import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.GradeDto;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.sports.dto.ProjectCategoryDto;
import com.wkbb.sports.dto.SettingDto;
import com.wkbb.sports.dto.TrainDto;
import com.wkbb.sports.model.HlifeProjectCategory;
import com.wkbb.sports.service.ProjectService;
import com.wkbb.sports.service.SettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(description = "项目接口")
@RequestMapping("/sports/{apiVersion}/project")
@Slf4j
public class ProjectController extends BaseController {

    @Autowired
    private ProjectService projectService;

    @ApiOperation(value = "新增或更新项目")
    @RequestMapping(value = "copeProject", method = RequestMethod.POST)
    public ResultDTO copeProject(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @RequestBody ProjectCategoryDto projectCategoryDto)
    {
    	UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}

        try {
        	projectCategoryDto.setCreateUser(userDto.getName());
        	projectService.addProject(projectCategoryDto);
            return new ResultDTO();
        } catch (Exception e) {
            log.error("***********addProject******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    
    @ApiOperation(value = "查询项目")
    @RequestMapping(value = "selProject", method = RequestMethod.GET)
    public ResultDTO<HlifeProjectCategory> selProject(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "项目ID", required = true) @RequestParam(name = "id", required = true) Long id)
    {
    	UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		HlifeProjectCategory  hlifeProjectCategory;
        try {
        	hlifeProjectCategory = projectService.selProject(id);
            return new ResultDTO(hlifeProjectCategory);
        } catch (Exception e) {
            log.error("***********addProject******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
    
    
    
    @ApiOperation(value = "查询项目列表")
    @RequestMapping(value = "selProjectList", method = RequestMethod.GET)
    public ResultDTO<List<HlifeProjectCategory>> selProjectList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion
            )
    {
    	UserDto userDto = this.getUserByLogin();
		if(null == userDto) {
			return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
		}
		List<HlifeProjectCategory>  list;
        try {
        	list = projectService.selProjectList();
            return new ResultDTO(list);
        } catch (Exception e) {
            log.error("***********addProject******", e);
            return new ResultDTO(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
    
   
   
}
